<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Produksi extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library(array('form_validation','server_processing','calendar_id'));
		$this->load->helper(array('form','url'));
		$this->load->model('produksi_model','',TRUE);
	}
	
	function index()
	{
		$partial['_header']='template/header';
		$partial['_sidebar']="template/sidebar";
		$partial['_contents']="produksi/produksi";	
		$data['title']='Produksi';
		
		$notif = $this->uri->segment(3);
		if($notif == '1')
		$data['notif'] = 'Data berhasil ditambah';
		else if($notif == '2')
		$data['notif'] = 'Data berhasil dihapus';
		else if($notif == '3')
		$data['notif'] = 'Update data success';
		else
		$data['notif'] = '';
		$this->template->load('index',$partial,$data);
	}
	
	function tampil_data()
	{
		//$data['id_barang'];
		$segmen = $this->uri->segment(3);
		$data['tahun'] = gmdate('Y', time()+60*60*7);
		
		if($this->uri->segment(4)!=0 && $this->uri->segment(5)!=0 && $this->uri->segment(6)!=0)
		{
			$tanggal = $this->uri->segment(5);
			$bulan = $this->uri->segment(4);
			$nama_hari = $this->uri->segment(6);
			
			if($nama_hari > 6) $nama_hari = 0;
			else if($nama_hari < 0) $nama_hari = 6;
			
			$data['tanggal'] = $tanggal + $segmen;
			$data['bulan'] = $bulan;
			$data['nama_hari'] = $nama_hari;
		}
		else
		{
			$data['tanggal'] = gmdate('d', time()+60*60*7)+$segmen;
			$data['bulan'] = gmdate('m', time()+60*60*7);
			$data['nama_hari'] = (gmdate('w', time()+60*60*7)+$segmen)%7;
		}
		
		//validasi beda bulan
		$data['jumlah_hari'] = $this->calendar_id->jumlah_hari($data['bulan'],$data['tahun']);
		if($data['tanggal'] < 1)
		{
			$data['bulan']-=1;
			$data['jumlah_hari'] = $this->calendar_id->jumlah_hari($data['bulan'],$data['tahun']);
			$data['tanggal'] = $data['jumlah_hari']+$data['tanggal'];
			if($data['bulan'] < 1)
			{
				$data['tahun']-=1;
				$data['bulan']= 12;
			}
			$data['jumlah_hari'] = $this->calendar_id->jumlah_hari($data['bulan'],$data['tahun']);
		}
		else if($data['tanggal'] > $data['jumlah_hari'])
		{
			$data['tanggal'] = 1;
			$data['bulan']+=1;
			if($data['bulan'] > 12)
			{
				$data['tahun']+=1;
				$data['bulan']= 1;
			}
			$data['jumlah_hari'] = $this->calendar_id->jumlah_hari($data['bulan'],$data['tahun']);
		}
		
		$data['query'] = $this->produksi_model->get_data();
		
		$this->load->view('produksi/tampil_data',$data);
	}
	
	
}